import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '../utils/storage'//导入缓存token公共函数
Vue.use(VueRouter)


const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login/index.vue')
  },
  {
    path: '/',
    name: 'layout',
    redirect: '/dashboard',
    component: () => import('../views/layout/index.vue'),
    children: [
      {
        //二级数据看板页面
        path: '/dashboard',
        name: 'dashboard',
        component: () => import('../views/dashBoard/index.vue')
      },
      {
        //二级文章管理页
        path: '/article',
        name: 'article',
        component: () => import('../views/article/index.vue')
      },

    ]
  },
  // {
  //   path: '/about',
  //   name: 'about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // }
]

const router = new VueRouter({
  routes
})

let whileList = ['/login']

router.beforeEach((to, form, next) => {
  let token = getToken()
  console.log(token);
  if (token) {
    next()
  } else {
    console.log(to.path);
    if (whileList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})
//保存原型对象的Push
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

export default router
